import { Injectable, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import DictService from 'src/system/service/DictService';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-07 21:08:51
 * @LastEditTime: 2024-12-07 22:35:55
 * @Description: file content
 */
@Injectable()
class DictUtil implements OnModuleInit {
  private static SYS_OPER_TYPE = 'sys_oper_type';
  private static dictService: DictService;

  constructor(private moduleRef: ModuleRef) {}

  onModuleInit() {
    //ModuleRef 允许你在运行时动态地访问其他模块中的提供器
    const dictServiceInstance = this.moduleRef.get(DictService, {
      strict: false,
    });
    if (dictServiceInstance) {
      DictUtil.dictService = dictServiceInstance;
    }
  }

  static async getDictLabel(dictType, dictValue) {
    const dicts = await DictUtil.dictService.findDictDataByType(dictType);
    for (const dict of dicts) {
      if (dict.dictValue == dictValue) {
        return dict.dictLabel;
      }
    }
  }

  static async getOperatorTypeLabel(value) {
    return await DictUtil.getDictLabel(DictUtil.SYS_OPER_TYPE, value);
  }
}
export default DictUtil;
